﻿using Inaction.ServiceContract;
using Inaction.Core;
using Inaction.Mapping;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Windows;

namespace Inaction.Business
{
    public partial class EntityList<T, Detail> : IEntityList
        where T : EntityList<T, Detail>, new()
        where Detail : Entity<Detail>, new()
    {

        #region Inaction.IEntityList
        public bool IsDirty
        {
            get
            {
                if (deletedEntities.Count > 0) return true;
                foreach (var item in this)
                {
                    if (item.IsDirty) return true;
                }
                return false;
            }
        }
        public bool CheckErrors(out string error)
        {
            return CheckErrors(out error, (System.Data.IDbTransaction)null);
        }
        public bool CheckErrors(out string error, System.Data.IDbTransaction trans = null)
        {
            error = string.Empty;
            foreach (var item in this)
            {
                var temp = string.Empty;
                if (item.CheckError(out temp, trans))
                {
                    error += temp;
                }
            }
            CheckUnique(out error, trans);
            return !string.IsNullOrEmpty(error);
        }
        public bool CheckUnique(out string error, System.Data.IDbTransaction trans = null)
        {
            error = string.Empty;
            var classInfo = ClassInfo.LoadInfo(typeof(Detail));
            foreach (var item in classInfo.UniqueProperties)
            {
                foreach (var p in item.Value)
                {
                }
            }
            return false;
        }
        public void CancelEdit()
        {
            for (int i = 0; i < this.Count; i++)
            {
                var item = this[i];
                if (item.IsNew)
                {
                    this.Remove(item);
                    i--;
                }
                else
                {
                    item.CancelEdit();
                }
            }
            foreach (var item in DeletedEntites)
            {
                this.Insert(item.Index, (Detail)item.Entity);
                item.Entity.CancelEdit();
                item.Entity.MarkNone();
            }
            firstTransData.Clear();
            lastTransData.Clear();
            DeletedEntites.Clear();
        }

        [Browsable(false)]
        public IEntity Master { get; set; }
        Inaction.Core.IEntity Inaction.Core.IChild.Master
        {
            get { return this.Master; }
            set { this.Master = (IEntity)value; }
        }
       
        #endregion

        #region 排序


        #endregion

        public T Clone()
        {
            var t = new T();
            foreach (var item in this)
            {
                t.Add(item.Clone());
            }
            return t;
        }
        object IClonable.Clone()
        {
            return Clone();
        }

        object IEntityList.AddNew()
        {
#if !WPF
            return base.AddNew();
#else
            return AddNew();
#endif
        }
        public Detail AddNew(Detail source)
        {
            var detail = new Detail();
            detail.Clone(source);
            this.Add(detail);
            return detail;
        }


        public virtual bool AllowQuery
        {
            get { return true; }
        }
    }
}